package com.cykj.pos.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.cykj.pos.controller.TestDTO.HttpClientHandler;
import com.cykj.pos.util.PosOneUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.cykj.pos.mapper.BizFeesChargedMapper;
import com.cykj.pos.domain.BizFeesCharged;
import com.cykj.pos.service.IBizFeesChargedService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流量费费用收取列Service业务层处理
 *
 * @author xuxiaolong
 * @date 2022-04-29
 */
@Service
public class BizFeesChargedServiceImpl extends ServiceImpl<BizFeesChargedMapper, BizFeesCharged> implements IBizFeesChargedService {
    @Resource
    private BizFeesChargedMapper bizFeesChargedMapper;
    @Resource
    private IBizFeesChargedService bizFeesChargedService;

    @Override
    public List<BizFeesCharged> queryList(BizFeesCharged bizFeesCharged) {
        LambdaQueryWrapper<BizFeesCharged> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(bizFeesCharged.getOrgnname())) {
            lqw.like(BizFeesCharged::getOrgnname, bizFeesCharged.getOrgnname());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getMerchantNo())) {
            lqw.eq(BizFeesCharged::getMerchantNo, bizFeesCharged.getMerchantNo());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getSn())) {
            lqw.eq(BizFeesCharged::getSn, bizFeesCharged.getSn());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getSimcardfee())) {
            lqw.eq(BizFeesCharged::getSimcardfee, bizFeesCharged.getSimcardfee());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getMamountStarts())) {
            lqw.eq(BizFeesCharged::getMamountStarts, bizFeesCharged.getMamountStarts());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getAmtType())) {
            lqw.eq(BizFeesCharged::getAmtType, bizFeesCharged.getAmtType());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getCreatdate())) {
            lqw.eq(BizFeesCharged::getCreatdate, bizFeesCharged.getCreatdate());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getUuid())) {
            lqw.eq(BizFeesCharged::getUuid, bizFeesCharged.getUuid());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getRmbStatus())) {
            lqw.eq(BizFeesCharged::getRmbStatus, bizFeesCharged.getRmbStatus());
        }
        if (StringUtils.isNotBlank(bizFeesCharged.getHostDate())) {
            lqw.eq(BizFeesCharged::getHostDate, bizFeesCharged.getHostDate());
        }
        return this.list(lqw);
    }

    @Override
    public List<BizFeesCharged> selectBizFeeCharged() {
        return bizFeesChargedMapper.selectBizFeeCharged();
    }

    @Override
    public void insertBizFeesCharged(BizFeesCharged bizFeesCharged) {
        bizFeesChargedMapper.insertBizFeesCharged(bizFeesCharged);
    }

    public void  timerGetChargeList(String posSn) throws Exception {
        //查询所有的设备
        System.out.println("------------------------------");
        System.out.println("-----------报件 绑定 查询费用收取列表-------------------"+new Date());
        System.out.println("------------------------------");
        //  List<BizPosMachine> bizPosMachineList = bizPosMachineService.timerGetAllPos();
        //   for (BizPosMachine bizPosMachine : bizPosMachineList) {
        //循环查询费用收取列表
        //  String snCode = bizPosMachine.getPosCode();
        String snCode = posSn;
        Map<String, String> data2 = new HashMap<String, String>();
        String url2 = "http://wx.wenwenpay.com.cn:6080/vipf/merchantinfo.do?action=handleAction&beanName=teramtDao&Methods=list";
        data2.put("sn", snCode);
        data2.put("mersource", "OUTMER");
        data2.put("agentid", "NP2T1215");
        data2.put("key", "c2cad9ae77f74e618ae672b122ee6496");
        data2.put("sign", PosOneUtil.generateSignature(data2));
        System.out.println("----------" + PosOneUtil.generateSignature(data2));
        System.out.println("---------" + data2);
        String result2 = HttpClientHandler.noPictureRequest(url2, data2);
        System.out.println("返回:" + result2);
        JSONObject jsonObject2 = JSONObject.parseObject(result2);
        String retCode2 = jsonObject2.getString("ret_code");
        JSONObject data4 = jsonObject2.getJSONObject("data");
        Integer totcount = data4.getInteger("totcount");
        JSONArray list = data4.getJSONArray("list");
        if (retCode2.equals("0") ) { //判断是否获取成功
            if (totcount > 0) { //判断是否查询到条数
                if (list.size() > 0) {  // 判断查寻到的list是否有值
                    for (int i = 0; i < list.size(); i++) {
                        JSONObject jsonObject1 = list.getJSONObject(i);
                        String mamount_id = jsonObject1.getString("mamount_id"); //费用ID(隐藏)
                        String orgnname = jsonObject1.getString("orgnname");   //所属代理
                        String merchant_no = jsonObject1.getString("merchant_no");  //系统商户编号
                        String sn = jsonObject1.getString("sn");  //SN
                        String simcardfee = jsonObject1.getString("simcardfee");  //收取费用
                        String mamount_starts = jsonObject1.getString("mamount_starts");  //状态(0-初始 1-成功 2-取消 3-待收取 9-未知)
                        String amt_type = jsonObject1.getString("amt_type");  //费用类型（0-流量费 1-设备款）
                        String creatdate = jsonObject1.getString("creatdate");  //创建日期
                        //查询成功 查询的信息存到 费用收取列表
                        if (mamount_starts.equals("1") &&  amt_type.equals("0")) {
                            BizFeesCharged bizFeesCharged = new BizFeesCharged();
                            bizFeesCharged.setMamountId(mamount_id);
                            bizFeesCharged.setOrgnname(orgnname);
                            bizFeesCharged.setMerchantNo(merchant_no);
                            bizFeesCharged.setSn(sn);
                            bizFeesCharged.setSimcardfee(simcardfee);
                            bizFeesCharged.setMamountStarts(mamount_starts);
                            bizFeesCharged.setAmtType(amt_type);
                            bizFeesCharged.setCreatdate(creatdate);
                            bizFeesCharged.setUuid(IdWorker.getIdStr());
                            BizFeesCharged byId = bizFeesChargedService.getById(mamount_id);
                            if (byId == null){
                                bizFeesChargedService.insertBizFeesCharged(bizFeesCharged);
                            }
                        }
                    }
                } else {
                    System.out.println("没有list");
                }
            }
        }
        // }
    }

    @Override
    public void updateStau(String uuid) {
        bizFeesChargedMapper.updateStau(uuid);
    }

    @Override
    public int getFeeChargeServiceByPosCode(String pos_sn, String merchantNo) {
        return bizFeesChargedMapper.getFeeChargeServiceByPosCode(pos_sn,merchantNo);
    }

    @Override
    public BizFeesCharged getLklLlf(BizFeesCharged bizFeesCharged) {
        return bizFeesChargedMapper.getLklLlf(bizFeesCharged);
    }
    @Override
    public BizFeesCharged selectNewOrderNo(String newOrderNo) {
        return bizFeesChargedMapper.selectNewOrderNo(newOrderNo);
    }

    @Override
    public int selectIsSecondSimAmt(String merchant_no,String sn) {
        return bizFeesChargedMapper.selectIsSecondSimAmt(merchant_no,sn);
    }

    @Override
    public BizFeesCharged selectCashbackAmountAll(Long merchId,String channel) {
        return bizFeesChargedMapper.selectCashbackAmountAll(merchId,channel);
    }

    @Override
    public BizFeesCharged selectCashbackAmountAllUp(Long merchId, String channel, String startTime, String endTime) {
        return bizFeesChargedMapper.selectCashbackAmountAllUp(merchId,channel,startTime,endTime);    }
}
